﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using MongoDB.Driver.GridFS;
using MongoDB.Driver.Linq;
using MongoDB.Bson;

namespace DBServiceProvider.MongoDB
{
    public class MongoOperation
    { 
        public static MongoCollection<T> collection<T>(string dbname, string collectionName)
        {
            return MongoDBClient.GetCollection<T>(collectionName, dbname);
        }
        public static WriteConcernResult InsertItem<T>(T item,string collectionName,string dbname)
        {
            return MongoOperation.collection<T>(dbname, collectionName).Insert<T>(item);
        }
        public static List<WriteConcernResult> InsertItems<T>(List<T> items,string collectionName,string dbname)
        {
            return MongoOperation.collection<T>(dbname, collectionName).InsertBatch<T>(items.AsEnumerable<T>()).ToList();
        }
        public static T FindItem<T>(IMongoQuery query,string collectionName,string dbname)
        {
            return MongoOperation.collection<T>(dbname, collectionName).FindOne(query);
        }
        public static List<T> FindItems<T>(IMongoQuery query,string collectionName,string dbname)
        {
            List<T> list = new List<T>();
            MongoCursor<T> cursor = MongoOperation.collection<T>(dbname, collectionName).Find(query);
            foreach(T item in cursor)
            {
                list.Add(item);
            }
            return list;
        }
        public static WriteConcernResult UpdateEntity<T>(IMongoQuery query, IMongoUpdate update, string collectionName, string dbname)
        {
            return MongoOperation.collection<T>(dbname, collectionName).Update(query, update);
        }
        public static WriteConcernResult RemoveEntity<T>(IMongoQuery query, string collectionName, string dbname)
        {
            return MongoOperation.collection<T>(dbname, collectionName).Remove(query);
        }
    }
}
